package com.second.service.impl;

import com.second.consts.PersonalConst;
import com.second.mapper.FCollectionMapper;
import com.second.mapper.FShopMapper;
import com.second.mapper.FShopPictureMapper;
import com.second.model.dto.personal.FShopCollection;
import com.second.model.pojo.FCollection;
import com.second.model.pojo.FShop;
import com.second.service.IFCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FCollectionServiceImpl implements IFCollectionService {

    @Autowired
    private FCollectionMapper collectionMapper;

    @Autowired
    private FShopMapper shopMapper;

    @Autowired
    private FShopPictureMapper shopPictureMapper;

    /**
     * 添加收藏
     * @param shopId
     * @param userId
     * @return
     */
    @Override
    public int addCollection(Integer shopId, Integer userId) {
        FCollection c1 = collectionMapper.findByUserIdAndShopId(userId, shopId);
        if (null == c1) {
            FCollection c = new FCollection();
            c.setShopId(shopId);
            c.setUserId(userId);
            c.setCreateDate(new Date());
            c.setStatus(PersonalConst.Collected_Status);
            collectionMapper.save(c);
            return 1;
            } else {
            c1.setCreateDate(new Date());
            c1.setStatus(PersonalConst.Collected_Status);
            collectionMapper.save(c1);
            return 1;
        }
    }

    /**
     * 取消收藏
     * @param shopId
     * @param userId
     * @return
     */
    @Override
    public int cancelCollection(Integer shopId, Integer userId) {
        FCollection c = collectionMapper.findByUserIdAndShopId(userId,shopId);
        c.setStatus(PersonalConst.Not_Collected_Status);
        c.setCreateDate(new Date());
        if (null!=collectionMapper.save(c)){
            return 1;
        }else
            return 0;
    }

    /**
     * 分页的收藏列表
     * @param userId
     * @param pageNum
     * @return
     */
    @Override
    public List<FShopCollection> findCollectionsPage(Integer userId, Integer pageNum) {
        Pageable pageable = PageRequest.of(pageNum,4, Sort.Direction.DESC, "createDate");
        //当前页面收藏店铺的集合
        Page<FCollection> page = collectionMapper.findAllByUserIdAndStatus(userId, PersonalConst.Collected_Status,pageable);
        //将当前页面的收藏店铺的内容和其他表加起来放到FShopCollection对象中返回到页面
        List<FShopCollection> list = new ArrayList<>();
        page.get().forEach(c->{
            FShop shop = shopMapper.getById(c.getShopId());
            FShopCollection fcShop = new FShopCollection();
            fcShop.setShopName(shop.getShopName());
            fcShop.setShopTelnumber(shop.getShopTelnumber());
            String picture = shopPictureMapper.findByShopId(c.getShopId()).getUrl1();
            fcShop.setShopPicture(picture);
            fcShop.setProvince(shop.getProvince());
            fcShop.setCity(shop.getCity());
            fcShop.setArea(shop.getArea());
            fcShop.setPlace(shop.getPlace());
            fcShop.setBusinessTime(shop.getBusinessTime());
            fcShop.setConsumption(shop.getConsumption());
            fcShop.setCreateDate(c.getCreateDate());
            fcShop.setShopId(c.getShopId());
            fcShop.setUserId(c.getUserId());
            fcShop.setCount(pageNum+1);
            fcShop.setPage(page.getTotalPages());
            list.add(fcShop);
        });
        return list;
    }

}
